<!DOCTYPE html>
<html>

<head>
    <title>编辑游记</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">

    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/editTravels.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/plugins/form/jquery.form.js"></script>
    <script src="/js/My97DatePicker/WdatePicker.js"></script>
    <script src="/js/common.js"></script>
    <script src="/js/checkLogin.js"></script>
    <script src="../js/plugins/eleditor/webuploader.min.js"></script>
    <script src="../js/plugins/eleditor/Eleditor.min.js"></script>
    <script src="../js/My97DatePicker/WdatePicker.js"></script>
    <script>

        $(function () {
            var isPublic;//游记的公开状态
            //地区回显
            $.get(baseUrl + "regions", function (data) {
                var temp = '';
                $.each(data, function (index, value) {
                    //拼接option
                    temp += '<option value="' + value.id + '">' + value.name + '</option>'
                })
                $("#regions").html(temp);
                //编辑页面回显
                var param = getParams();
                //通过id去查询当前游记的信息
                if (param.id) {
                    $.get(baseUrl + "travels/" + param.id, function (data) {
                        isPublic = data.isPublic;
                        $(".travels").renderValues(data)
                    })
                }

            })
            //监听公开状态
            $("#detailForm input[name='isPublic']").click(function () {
                isPublic = !isPublic;
            })


            //使用富编辑插件
            var Edr = new Eleditor({
                el: '#contentEditor',
                upload: {
                    server: baseUrl + 'images',
                },
            });
            //绑定更换封面上传事件
            $("#changeBtn").click(function () {
                $("#images input[name='file']").click()
            })
            //读取图片上传事件来发送请求
            $("#images input[name='file']").change(function (data) {
                //如果有值就提交表单
                if ($(this).val()) {
                    $("#images").ajaxSubmit({
                        url: baseUrl + "images",
                        success: function (data) {
                            if (data.status) {
                                //回显背景,并把地址值设置在隐藏域中
                                $("#coverUrl").attr("src", data.url);
                                $("#detailForm input[name='coverUrl']").val(data.url);
                            }

                        }
                    })
                }

            });
            //抽取保存和编辑的提交事件
            $("#detailForm").ajaxForm({
                url: baseUrl + "travels",
                beforeSubmit: function (arr) {
                    arr.push({name: "title", value: $("#title").val()});
                    arr.push({name: "travelContent.content", value: Edr.getContent()});
                    //如果有id则是更新操作,发送put请求
                    if (getParams().id) {
                        arr.push({name: "_method", value: "put"});
                        arr.push({name: "id", value: getParams().id})
                    }
                },
                success: function (data) {
                    if (data.success) {
                        //保存成功跳转到我的游记界面
                        window.location.href = "travels.html";

                    } else {
                        $(document).dialog({
                            type: 'confirm',
                            style: 'android',
                            titleText: '温馨提示:',
                            content: "更新失败!失败原因:" + data.errorMsg,
                            buttons: [
                                {
                                    name: '返回',
                                    callback: function () {
                                    }
                                }
                            ]
                        });
                    }
                }
            })

            //绑定保存按钮提交表单事件
            $("#save").click(function () {
                //如果是保存按钮,把状态改为草稿 state = 0
                $("#state").val("0");

                $("#detailForm").submit();
            });
            //绑定保存按钮提交表单事件
            $("#release").click(function () {

                //如果是发布且是公开状态,则把状态改为待审核 state=1
                if (isPublic) {
                    $("#state").val("1");
                    $("#detailForm").submit();
                } else {
                    $(document).dialog({
                        type: 'confirm',
                        closeBtnShow: true,
                        content: '温馨提示:发布游记内容将会对所有人公开,点击确认发布游记,取消改为只对自己可见但不会发布',
                        onClickConfirmBtn: function () {
                            //如果确认发布,状态改为1,isPublic改为true
                            $("#state").val("1");
                            $("#detailForm input[name='isPublic']").prop('checked', true);
                            $("#detailForm").submit();
                        },
                        onClickCancelBtn: function () {
                            $("#state").val("0");
                            $("#detailForm").submit();
                        },
                        onClickCloseBtn: function () {
                            $("#state").val("0");
                            $("#detailForm").submit();
                        }
                    });

                }

            });

            //日历插件
            $("#detailForm input[name='travelTime']").click(function () {
                WdatePicker({
                    readOnly: true, //只读
                    maxDate: new Date() //最多选到今天
                });
            });

        })
    </script>


</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:window.history.go(-1);">
                <span><i class="fa fa-times"></i></span>
            </a>
        </div>
        <div class="col-5"></div>
        <div class="col" style="text-align: right;">
            <i class="fa fa-floppy-o fg-fw" id="save"><span>保存</span></i>
            <i class="fa fa-check" id="release"><span>发布</span></i>
        </div>
    </div>
</div>

<div class="travels form-group">
    <div class="cover">
        <img id="coverUrl" width="100%" height="200px" render-src="coverUrl"/>

        <button type="button" class="btn" id="changeBtn">
            <i class="fa fa-pencil-square-o"></i>
            <small>更换封面</small>
        </button>


    </div>
    <!--更换封面的隐藏上传标签-->
    <form id="images" enctype="multipart/form-data" method="post">
        <input type="file" name="file" hidden>
    </form>

    <div class="title">
        <input class="form-control" id="title" placeholder="请输入标题" render-value="title"/>
    </div>
    <h6 href="#">点击文章内容即可编辑</h6>
    <div id="contentEditor" render-html="travelContent.content">
    </div>
    <hr/>
    <div class="container">
        <form id="detailForm" method="post">
            <!--保存图片的地址-->
            <input type="hidden" name="coverUrl" render-value="coverUrl">
            <input type="hidden" id="state" name="state"/>

            <label>出行时间:</label>
            <input type="text" name="travelTime" class="form-control" render-value="travelTime">
            <label>出行天数:</label>
            <input type="number" name="days" class="form-control" render-value="days">
            <label>参与人物:</label>
            <select class="c-select form-control" name="person" render-value="person">
                <option value="1" selected>一个人的旅行</option>
                <option value="2">和父母</option>
                <option value="3">和朋友</option>
                <option value="4">和同事</option>
                <option value="5">和爱人</option>
                <option value="6">和其他</option>
            </select>
            <label>人均费用:</label>
            <input type="text" name="perExpends" class="form-control" render-value="perExpends">

            <label>旅游地点:</label>
            <select class="form-control" id="regions" render-value="place.id" name="place.id">

            </select>

            <div class="form-check">
                <label class="form-check-label">
                    <input class="form-check-input" type="checkbox" name="isPublic" render-attr="checked=isPublic"
                    >是否公开
                </label>
            </div>

        </form>
    </div>

    <hr/>

</div>


</body>

</html>